#coding: utf-8
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
import time
import os
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By

DEFAULT_SECONDS=2
the_path=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

class BasePage():

	
	#驱动,url页面地址
	def __init__(self,driver,sub_url):
		self.driver=driver
		self.url='http://localhost/wordpress/'+sub_url
		self.driver.get(self.url)

	#id定位元素,定位失败则自动截图
	def by_id(self,the_id):
		#显示等待
		try:
			WebDriverWait(self.driver, DEFAULT_SECONDS).until(EC.visibility_of_element_located((By.ID,the_id)))
		except TimeoutException as e:
			self.driver.get_screenshot_as_file(the_path+'/screenshots/%s.png'  %time.time())
			msg="找不到元素"+the_id
			raise TimeoutException(msg)

		#隐式等待
		#driver.implicitly_wait(2)
		return self.driver.find_element_by_id(the_id)
		